var prefix = jsctx + "/exceptionOrder";

var exceptionOrderInfo = (function () {
    function init() {
        setTimeout(function () {//防止先于-读取缓存执行
            load();
        }, 10)
    };

    $('#exampleTable').on('load-success.bs.table', function (e, data) {
        if (data.total && !data.rows.length) {
            $('#exampleTable').bootstrapTable('selectPage').bootstrapTable('refresh');
        }
    });
    var h = $(window).height() - 80;

    function load() {

        var userTenantCode = $("#userTenantCode").val();
        $('#exampleTable')
            .bootstrapTable(
                {
                    method: 'get',
                    url: prefix + "/listExceptionOrderInfo",
                    iconSize: 'outline',
                    toolbar: '#exampleToolbar',
                    striped: false,
                    dataType: "json",
                    pagination: true,
                    singleSelect: false,
                    // pageSize: 10,
                    pageList: [10, 20, 30],
                    pageNumber: 1,
                    sidePagination: "server",
                    paginationShowPageGo: true,
                    height: h,
                    rightFixedColumns: true,
                    showColumns: true,
                    queryParams: function (params) {
                        return {
                            limit: params.limit,
                            offset: (params.offset / params.limit) + 1,
                            status: $("#status").val().trim(),
                            nickname: $("#nickname").val().trim(),
                            startTime: $("#startTime").val().trim(),
                            endTime: $("#endTime").val().trim(),
                            mobile: $("#mobile").val().trim(),
                            orderNum: $("#orderNum").val().trim(),
                            gunNo: $("#gunNo").val().trim(),
                            stationName:$("#stationName").val().trim(),
                            tenantOperator:$("#tenantOperator").val().trim(),

                        };
                    },
                    columns: [
                        {
                            title: '#', field: '', width: '50', align: 'left',
                            formatter: function (value, row, index) {
                                return index + 1;
                            }
                        },
                        {
                            field: 'status', title: '状态', width: '60', align: 'left',
                            formatter: function (value, row, index) {
                                if (value == '0') {
                                    return '<span >未充电</span>'
                                } else if (value == '1') {
                                    return '<span >充电中</span>'
                                } else if (value == '2') {
                                    return '<span >完成</span>'
                                } else if (value == '3') {
                                    return '<span >充电结算中</span>'
                                }
                            }
                        },
                        {
                            title: '互联运营商', field: 'hlhtOperatorName', width: '110', align: 'left',
                            formatter: function (value, row, index) {
                                if (row.orderInfoHlhtType == '0') {
                                    if ((row.hlhtTenantCode != null && row.hlhtTenantCode != '') && (row.hlhtOperatorId == null || row.hlhtOperatorId == '')) {
                                        return row.hlhtTenantName
                                    } else {
                                        return row.hlhtOperatorName
                                    }
                                } else if (row.orderInfoHlhtType == '1') {
                                    return row.hlhtTenantName
                                } else {
                                    return row.hlhtTenantName
                                }
                            }
                        },
                        {
                            title: '所属分销商' ,field: 'operatorName',width:'150',align: 'left',
                        },
                        {
                            field: 'hlhtOrderType', title: '订单类型', width: '85', align: 'left',
                            formatter: function (value, row, index) {
                                if (row.orderInfoHlhtType == '0') {
                                    if (row.hlhtOrderType == '0') {
                                        return '<span >自有订单</span>'
                                    } else if (row.hlhtOrderType == '1') {
                                        return '<span >反向订单</span>'
                                    } else if (row.hlhtOrderType == '2') {
                                        return '<span >正向订单</span>'
                                    }
                                } else if (row.orderInfoHlhtType == '1') {
                                    if (row.hlhtOrderType == '1') {
                                        return '<span >反向订单</span>'
                                    } else if (row.hlhtOrderType == '2') {
                                        return '<span >正向订单</span>'
                                    }
                                } else if (row.orderInfoHlhtType == '2') {
                                    return '<span >正向订单</span>'
                                }
                            }
                        },
                        {
                            field: 'hlhtOrderType', title: '互联类型', width: '85', align: 'left',
                            formatter: function (value, row, index) {
                                if (row.orderInfoHlhtType == '0') {
                                    if (value == '0') {
                                        return '<span ></span>'
                                    } else if ((row.hlhtTenantCode != null && row.hlhtTenantCode != '') && (row.hlhtOperatorId == null || row.hlhtOperatorId == '')) {
                                        return '<span >内部互联</span>'
                                    } else {
                                        return '<span >外部互联</span>'
                                    }
                                } else if (row.orderInfoHlhtType == '1') {
                                    return '<span >内部互联</span>'
                                } else {
                                    return '<span >内部互联</span>'
                                }
                            }
                        },
                        {field: 'stationName', title: '站点名称', width: '120', align: 'left'},

                        {
                            field: 'companyName', title: '所属公司', width: '120', align: 'left',
                            formatter: function (value, row, index) {
                                if (row.orderInfoHlhtType == '0') {
                                    if (row.hlhtOrderType == '0') {
                                        return value
                                    } else if (row.hlhtOrderType == '1') {
                                        return value
                                    } else if (row.hlhtOrderType == '2') {
                                        return ''
                                    }
                                } else if (row.orderInfoHlhtType == '1') {
                                    if (row.hlhtOrderType == '1') {
                                        return value
                                    } else if (row.hlhtOrderType == '2') {
                                        return ''
                                    }
                                } else if (row.orderInfoHlhtType == '2') {
                                    return ''
                                }
                            }
                        },
                        {field: 'gunNo', title: '枪编码', width: '85', align: 'left'},
                        {field: 'orderNum', title: '订单号', width: '130', align: 'left'},
                        {field: 'outOrderNum', title: '外部订单号', width: '130', align: 'left'},
                        {field: 'nickname', title: '会员名', width: '80', align: 'left'},
                        {field: 'mobile', title: '手机号', width: '100', align: 'left'},
                        {field: 'carNum', title: '车牌', width: '90', align: 'left'},
                        {
                            field: 'accountType', title: '充电方式', width: '85', align: 'left',
                            formatter: function (value, row, index) {
                                if (value == '0') {
                                    return '<span >个人充电</span>'
                                } else if (value == '1') {
                                    return '<span >企业充电</span>'
                                } else if (value == '2') {
                                    return '<span >共享充电</span>'
                                }
                            }
                        },
                        {field: 'hisStartTime', title: '开始时间', width: '90', align: 'left'},
                        {field: 'hisEndTime', title: '结束时间', width: '90', align: 'left'},
                        {field: 'hisLastTime', title: '充电时长', width: '80', align: 'left'},
                        {field: 'hisPower', title: '电量<br>(度)', width: '65', align: 'right'},
                        {field: 'hisElecAmount', title: '电费<br>(元)', width: '65', align: 'right'},
                        {field: 'hisServiceAmount', title: '服务费<br>(元)', width: '70', align: 'right'},
                        {field: 'hisDiscountAmount', title: '优惠<br>(元)', width: '65', align: 'right'},
                        {
                            field: 'hisTotalMoney', title:
                                '总价<br>(元)', width:
                                '65', align:
                                'right'
                        }
                        ,
                        {
                            field: 'hisPayAmount', title:
                                '实付<br>(元)', width:
                                '65', align:
                                'right'
                        }
                        ,
                        /**
                         * 停止方式
                         * 0: 用户手动停止充电；
                         * 1: 客户归属地运营商平台停止充电；
                         * 2: BMS 停止充电；
                         * 3: 充电机网络异常；
                         * 4: 连接器断开
                         * 5: 电价异常
                         * 6: 余额不足
                         * 7: 电流过低停止
                         * 8: 充满平台停止
                         * 9: 充满停止
                         * 10: 电池温度过高停止
                         * 11: 电流过高
                         * 12: 电压过高
                         * 13: 订单已结算
                         * 14: 刷卡停止
                         * 15: 超时停止
                         */
                        {
                            field: 'hisBeginSoc', title:
                                '开始SOC', width:
                                '65', align:
                                'right'
                        }
                        ,
                        {
                            field: 'hisSoc', title:
                                '结束SOC', width:
                                '65', align:
                                'right'
                        }
                        ,
                        {
                            field: 'pileStopReasonVo', title:
                                '停止方式', width:
                                '100', align:
                                'left'
                        }
                        ,
                        {
                            field: 'stopReason', title:
                                '停止原因', width:
                                '100', align:
                                'left', class:
                                'operateBefore-td',
                            formatter:

                                function (value, row, index) {
                                    if (value == '0') {
                                        return '<span >手动停止充电</span>'
                                    } else if (value == '1') {
                                        return '<span >客户归属地运营商平台停止充电</span>'
                                    } else if (value == '2') {
                                        return '<span >BMS停止</span>'
                                    } else if (value == '3') {
                                        return '<span >充电机网络异常</span>'
                                    } else if (value == '4') {
                                        return '<span >连接器断开</span>'
                                    } else if (value == '5') {
                                        return '<span >电价异常</span>'
                                    } else if (value == '6') {
                                        return '<span >余额不足</span>'
                                    } else if (value == ' 7') {
                                        return '<span >电流过低停止</span>'
                                    } else if (value == '8') {
                                        return '<span >充满平台停止</span>'
                                    } else if (value == '9') {
                                        return '<span >充满停止</span>'
                                    } else if (value == '10') {
                                        return '<span >电池温度过高停止</span>'
                                    } else if (value == '11') {
                                        return '<span >电流过高</span>'
                                    } else if (value == '12') {
                                        return '<span >电压过高</span>'
                                    } else if (value == '13') {
                                        return '<span >订单已结算</span>'
                                    } else if (value == '14') {
                                        return '<span >刷卡停止</span>'
                                    } else if (value == '15') {
                                        return '<span >超时停止</span>'
                                    } else if (value == '16') {
                                        return '<span >温度阈值</span>'
                                    } else if (value == null) {
                                        return '<span ></span>'
                                    }
                                }
                        }
                        ,
                        {field:'exceptionStartTime',title:'开始时间',width:'90',align:'left'},
                        {field:'exceptionEndTime',title:'结束时间',width:'90',align:'left'},
                        {field:'exceptionLastTime',title:'充电时长',width:'80',align:'left'},
                        {field: 'exceptionPower', title: '电量<br>(度)', width: '65', align: 'right'},
                        {field: 'exceptionElecAmount', title: '电费<br>(元)', width: '65', align: 'right'},
                        {field: 'exceptionServiceAmount', title: '服务费<br>(元)', width: '70', align: 'right'},
                        {field: 'exceptionDiscountAmount', title: '优惠<br>(元)', width: '65', align: 'right'},
                        {
                            field: 'exceptionTotalMoney', title:
                                '总价<br>(元)', width:
                                '65', align:
                                'right'
                        }
                        ,
                        {
                            field: 'exceptionPayAmount', title:
                                '实付<br>(元)', width:
                                '65', align:
                                'right'
                        }
                        ,
                        /**
                         * 停止方式
                         * 0: 用户手动停止充电；
                         * 1: 客户归属地运营商平台停止充电；
                         * 2: BMS 停止充电；
                         * 3: 充电机网络异常；
                         * 4: 连接器断开
                         * 5: 电价异常
                         * 6: 余额不足
                         * 7: 电流过低停止
                         * 8: 充满平台停止
                         * 9: 充满停止
                         * 10: 电池温度过高停止
                         * 11: 电流过高
                         * 12: 电压过高
                         * 13: 订单已结算
                         * 14: 刷卡停止
                         * 15: 超时停止
                         */
                        {
                            field: 'exceptionBeginSoc', title:
                                '开始SOC', width:
                                '65', align:
                                'right'
                        }
                        ,
                        {
                            field: 'exceptionSoc', title:
                                '结束SOC', width:
                                '65', align:
                                'right'
                        }
                        ,
                        {
                            field: 'pileStopReasonVo', title:
                                '停止方式', width:
                                '100', align:
                                'left'
                        }
                        ,
                        {
                            field: 'stopReason', title:
                                '停止原因', width:
                                '100', align:
                                'left', class:
                                'operateBefore-td',
                            formatter:

                                function (value, row, index) {
                                    if (value == '0') {
                                        return '<span >手动停止充电</span>'
                                    } else if (value == '1') {
                                        return '<span >客户归属地运营商平台停止充电</span>'
                                    } else if (value == '2') {
                                        return '<span >BMS停止</span>'
                                    } else if (value == '3') {
                                        return '<span >充电机网络异常</span>'
                                    } else if (value == '4') {
                                        return '<span >连接器断开</span>'
                                    } else if (value == '5') {
                                        return '<span >电价异常</span>'
                                    } else if (value == '6') {
                                        return '<span >余额不足</span>'
                                    } else if (value == ' 7') {
                                        return '<span >电流过低停止</span>'
                                    } else if (value == '8') {
                                        return '<span >充满平台停止</span>'
                                    } else if (value == '9') {
                                        return '<span >充满停止</span>'
                                    } else if (value == '10') {
                                        return '<span >电池温度过高停止</span>'
                                    } else if (value == '11') {
                                        return '<span >电流过高</span>'
                                    } else if (value == '12') {
                                        return '<span >电压过高</span>'
                                    } else if (value == '13') {
                                        return '<span >订单已结算</span>'
                                    } else if (value == '14') {
                                        return '<span >刷卡停止</span>'
                                    } else if (value == '15') {
                                        return '<span >超时停止</span>'
                                    } else if (value == '16') {
                                        return '<span >温度阈值</span>'
                                    } else if (value == null) {
                                        return '<span ></span>'
                                    }
                                }
                        },
                        {field:'diffElecMoney',title:'电费差值',width:'100',align:'right'},
                        {field:'diffServiceMoney',title:'服务费差值',width:'100',align:'right'},
                        {field:'diffPayAmount',title:'实付差值',width:'100',align:'right'},
                        {field:'solution',title:'解决办法',width:'100',align:'left'},
                        {field:'repairTime',title:'修复时间',width:'90',align:'left'},
                        {
                            field:'exceptionStatus',
                            title:'状态',
                            width:'100',
                            align:'right',
                            formatter:function (value,row,index) {
                                if (value == '0') {
                                    return '<span >待复核</span>'
                                } else if (value == '1') {
                                    return '<span >待修复</span>'
                                } else if (value == '2') {
                                    return '<span >待核销</span>'
                                } else if (value == '3') {
                                    return '<span >待入账</span>'
                                } else if (value == '4') {
                                    return '<span >已入账</span>'
                                } else if (value == '5') {
                                    return '<span >已驳回</span>'
                                } else if (value == '6') {
                                    return '<span >已完成</span>'
                                }
                            }
                        },
                        {field:'exceptionLastUpdatedBy',title:'操作人',width:'100',align:'right'},
                        {field:'rejectionReason',title:'备注',width:'100',align:'right'},
                        {
                            title: '操作',
                            field:
                                'operate',
                            width:
                                '180',
                            align:
                                'center',
                            class:
                                'operate-td',
                            formatter:
                                function (value, row, index) {
                                    var a = '<a class="btn btn-success btn-sm' + has_edit + '" href="#" title="复核完成" onclick="exceptionOrderInfo.updateExceptionOrderStatus(\''
                                        + row.exceptionOrderInfoId + '\',\'' + 1
                                        + '\')">复核完成</a> ';

                                    var b = '<a class="btn btn-success btn-sm' + has_edit + '" href="#" title="修复完成" onclick="exceptionOrderInfo.updateExceptionOrderStatus(\''
                                        + row.exceptionOrderInfoId + '\',\'' + 2
                                        + '\')">修复完成</a> ';

                                    var c = '<a class="btn btn-success btn-sm ' + has_edit + '" href="#" title="核销完成" onclick="exceptionOrderInfo.updateExceptionOrderStatus(\''
                                        + row.exceptionOrderInfoId + '\',\'' + 6
                                        + '\')">核销完成</a> ';

                                    var d = '<a class="btn btn-success btn-sm' + has_edit + '" href="#" title="驳回" onclick="exceptionOrderInfo.updateExceptionOrderStatusForFail(\''
                                        + row.exceptionOrderInfoId
                                        + '\')">驳回</a> ';

                                    if(row.exceptionStatus=='0'){
                                        return a + d ;
                                    }else if (row.exceptionStatus=='1'){
                                        return b + d ;
                                    }else if (row.exceptionStatus=='2'){
                                        return c ;
                                    }else {
                                        return '';
                                    }
                                }
                        }
                    ],
                })
        ;
    }


    function exportOrderInfo() {
        var hlhtOperatorId;
        if ($("#hlhtOperatorId").length > 0) {
            hlhtOperatorId = $("#hlhtOperatorId").val();
        } else {
            hlhtOperatorId = '';
        }
        $.fileDownload(prefix + "/exportExceptionOrderInfo", { // 后台请求链接
            httpMethod: "get",　　　　　　　　// 请求方式
            contentType: 'application/json',　 // 请求头
            data: {　　　　　　　　　　　　　　　// 参数，左边为后台接收的参数名，右边为前端获取的值，
                status: $("#status").val().trim(),
                nickname: $("#nickname").val().trim(),
                startTime: $("#startTime").val().trim(),
                endTime: $("#endTime").val().trim(),
                mobile: $("#mobile").val().trim(),
                orderNum: $("#orderNum").val().trim(),
                gunNo: $("#gunNo").val().trim(),
                stationName:$("#stationName").val().trim(),
                tenantOperator:$("#tenantOperator").val().trim(),
                count: $("#count").val().trim() == '' ? 0 : $("#count").val().trim(),
                offset: $('.active .page-link').text(),
                limit: $(".page-size").text()
            },
            prepareCallback: function (url) {
                layer.msg("文件下载中...", {time: -1})
            },
            abortCallback: function (url) {
                // 异常终止
                layer.msg("文件下载异常！！");
            },
            successCallback: function (url) {
                layer.msg("文件下载成功！！", {time: 1500});
            },
            failCallback: function (data) {
                var str = "{" + data.substring(data.indexOf("{") + 1, data.indexOf("}")) + "}";
                layer.msg(JSON.parse(str).message);
            }
        })

    }


    function reLoad() {
        var startTime = $("#startTime").val();
        if (startTime == '' || startTime == null) {
            parent.layer.alert("请选择开始时间");
            return;
        }
        $(".fixed-table-header-columns").css("display", "none");
        $(".fixed-table-body-columns").css("display", "none");
        $('#exampleTable').bootstrapTable('refresh');
    }

    function goBack() {
        location.href = prefix + "/index";
    }

    function updateExceptionOrderStatus(exceptionOrderInfoId,status) {
        $.ajax({
            url: prefix + "/updateExceptionOrderStatus",
            type: "post",
            data:{
                "exceptionOrderInfoId":exceptionOrderInfoId,
                "status":status,
            },
            success:function (data) {
                if (data.status==200){
                    layer.msg("操作成功");
                    reLoad();
                } else {
                    layer.msg(data.message);
                }
            }
        });
    }
    function updateExceptionOrderStatusForFail(exceptionOrderInfoId) {
        $('#exampleModal').modal('show')
        $("#submitBtn").unbind("click").bind("click", function () {
            $.ajax({
                url: prefix + "/updateExceptionOrderStatus",
                type: "post",
                data: {
                    "exceptionOrderInfoId":exceptionOrderInfoId,
                    "rejectionReason":$("#rejectionReason").val(),
                    "status":"5",
                },
                success: function (data) {
                    if (data.status == 200) {
                        parent.layer.msg("操作成功");
                        $('#exampleModal').on('hidden.bs.modal', function () {
                            document.getElementById("contentForm").reset();
                        })
                        $('#exampleModal').modal('hide')
                        reLoad();
                    } else {
                        parent.layer.alert(data.message);
                        $('#exampleModal').on('hidden.bs.modal', function () {
                            document.getElementById("contentForm").reset();
                        })
                        $('#exampleModal').modal('hide')
                    }

                }
            })
        })
    }


    return {
        init: init,
        goBack: goBack,
        reLoad: reLoad,
        updateExceptionOrderStatus:updateExceptionOrderStatus,
        exportOrderInfo:exportOrderInfo,
        updateExceptionOrderStatusForFail:updateExceptionOrderStatusForFail,
    };


})();
